package com.echatim.demo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import com.echatim.sdk.EChatIMSDKBridge;
import com.echatim.sdk.IPfuncApiCb;
import com.echatim.sdk.IPfuncInitCb;
import com.echatim.sdk.IPfuncListenerCb;

/**
 *  echatim sdk 安卓版
 *  第三方库的标准输出(stdout,stderr)重定向到/sdcard/echatimsdklog/ 目录下, 框架出现问题时可以查看该目录log
 *
 * */
public class MainActivity extends AppCompatActivity {
    private  static final String LOG_TAG = MainActivity.class.getName();
    private  static boolean sdkInitFinished = false;
    private  int apiCallId = 1; // api 调用id, 实现方式可用时间戳, 或一个自增变量


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            mainDemo();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }



    private void sdkTestApis() throws InterruptedException {
        // 测试 'userGet'
        while (true){
            EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "userGet", "{\"auid\":\"admin\"}");

//        //  测试API
            EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "userUpdate", "{}");
            EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "userList", "{}");
            EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "userGet", "{}");
            EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "userUpdateToken", "{}");
            EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "userRefreshToken", "{}");
            EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "historyListMessage", "{}");
            EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "historyListSession", "{}");
            EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "addFriend", "{}");
            EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "delFriend", "{}");
            EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "listFriends", "{}");
            EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "getFriend", "{}");
            EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "modifyBlacklistForbid", "{}");
            EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "listBlackListForbid", "{}");
            EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "modifyAlias", "{}");
            EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "addRoom", "{}");
            EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "updateRoom", "{}");
            EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "listRoom", "{}");
            EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "listRoomJoin", "{}");
            EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "delRoom", "{}");
            EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "getRoom", "{}");
            EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "listRoomMember", "{}");
            EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "addRoomMember", "{}");
            EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "delRoomMember", "{}");
            EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "userOnline", "{}");

            Thread.sleep(10*1000);

        }

    }


    public final static IPfuncInitCb sdkInitCb = (ret, errMsg) -> {
        Log.v(LOG_TAG, "sdkInit ok.");
        if(ret == 0){
            sdkInitFinished = true;
        }
    };
    public final static IPfuncListenerCb sdkListenerCb = (msg, argv) -> {
        if(!"".equals(msg) && argv.length > 0){
            Log.v(LOG_TAG, String.format("sdk_listener_callback_handler: message = %s, argv[0]=%s", msg, argv[0]));
        }
    };

    public final static IPfuncApiCb sdkApiCb = (int callId, String method, String response) -> {
        if(!"".equals(method)){
            Log.v(LOG_TAG, String.format("api_listener_callback_handler: callId = %d,  method = %s, response = %s", callId, method, response));
        }
    };

    private void mainDemo() throws InterruptedException {
        String configJson = "{\"host\":\"api.echatim.cn\",\"httpPort\":58082,\"socketPort\":59092,\"key\":\"TSDKTEST00001\",\"secret\":\"\",\"apiTransport\":\"HTTP\",\"loginAuid\":\"admin\",\"loginToken\":\"admin\"}";
        Log.v(LOG_TAG, "android ready to sdkInit.");
        EChatIMSDKBridge.sdkInit(configJson, sdkInitCb, sdkListenerCb);
        // 等待sdk初始完成.
        while (!sdkInitFinished){
            Thread.sleep(100);
            System.out.print(".");
        }

        new Thread(()->{
            try {
                // api 回调处理
                EChatIMSDKBridge.sdkSetApiListener(sdkApiCb);
                sdkTestApis();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

    }
}
